home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / asynchat.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  6KB  |  216 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import socket
  5. import asyncore
  6. from collections import deque
  7.  
  8. class async_chat(asyncore.dispatcher):
  9.     ac_in_buffer_size = 4096
  10.     ac_out_buffer_size = 4096
  11.     
  12.     def __init__(self, conn = None):
  13.         self.ac_in_buffer = ''
  14.         self.ac_out_buffer = ''
  15.         self.producer_fifo = fifo()
  16.         asyncore.dispatcher.__init__(self, conn)
  17.  
  18.     
  19.     def collect_incoming_data(self, data):
  20.         raise NotImplementedError, 'must be implemented in subclass'
  21.  
  22.     
  23.     def found_terminator(self):
  24.         raise NotImplementedError, 'must be implemented in subclass'
  25.  
  26.     
  27.     def set_terminator(self, term):
  28.         self.terminator = term
  29.  
  30.     
  31.     def get_terminator(self):
  32.         return self.terminator
  33.  
  34.     
  35.     def handle_read(self):
  36.         
  37.         try:
  38.             data = self.recv(self.ac_in_buffer_size)
  39.         except socket.error:
  40.             why = None
  41.             self.handle_error()
  42.             return None
  43.  
  44.         self.ac_in_buffer = self.ac_in_buffer + data
  45.         while self.ac_in_buffer:
  46.             lb = len(self.ac_in_buffer)
  47.             terminator = self.get_terminator()
  48.             None if not terminator else lb < n
  49.             terminator_len = len(terminator)
  50.             index = self.ac_in_buffer.find(terminator)
  51.             if index != -1:
  52.                 if index > 0:
  53.                     self.collect_incoming_data(self.ac_in_buffer[:index])
  54.                 
  55.                 self.ac_in_buffer = self.ac_in_buffer[index + terminator_len:]
  56.                 self.found_terminator()
  57.                 continue
  58.             index = find_prefix_at_end(self.ac_in_buffer, terminator)
  59.             if index:
  60.                 if index != lb:
  61.                     self.collect_incoming_data(self.ac_in_buffer[:-index])
  62.                     self.ac_in_buffer = self.ac_in_buffer[-index:]
  63.                 
  64.                 break
  65.                 continue
  66.             self.collect_incoming_data(self.ac_in_buffer)
  67.             self.ac_in_buffer = ''
  68.  
  69.     
  70.     def handle_write(self):
  71.         self.initiate_send()
  72.  
  73.     
  74.     def handle_close(self):
  75.         self.close()
  76.  
  77.     
  78.     def push(self, data):
  79.         self.producer_fifo.push(simple_producer(data))
  80.         self.initiate_send()
  81.  
  82.     
  83.     def push_with_producer(self, producer):
  84.         self.producer_fifo.push(producer)
  85.         self.initiate_send()
  86.  
  87.     
  88.     def readable(self):
  89.         return len(self.ac_in_buffer) <= self.ac_in_buffer_size
  90.  
  91.     
  92.     def writable(self):
  93.         if self.ac_out_buffer == '' and self.producer_fifo.is_empty():
  94.             pass
  95.         return not (self.connected)
  96.  
  97.     
  98.     def close_when_done(self):
  99.         self.producer_fifo.push(None)
  100.  
  101.     
  102.     def refill_buffer(self):
  103.         while len(self.producer_fifo):
  104.             p = self.producer_fifo.first()
  105.             if p is None:
  106.                 if not self.ac_out_buffer:
  107.                     self.producer_fifo.pop()
  108.                     self.close()
  109.                 
  110.                 return None
  111.             elif isinstance(p, str):
  112.                 self.producer_fifo.pop()
  113.                 self.ac_out_buffer = self.ac_out_buffer + p
  114.                 return None
  115.             
  116.             data = p.more()
  117.             if data:
  118.                 self.ac_out_buffer = self.ac_out_buffer + data
  119.                 return None
  120.             else:
  121.                 self.producer_fifo.pop()
  122.             data
  123.             return None
  124.             continue
  125.             return None
  126.  
  127.     
  128.     def initiate_send(self):
  129.         obs = self.ac_out_buffer_size
  130.         if len(self.ac_out_buffer) < obs:
  131.             self.refill_buffer()
  132.         
  133.         if self.ac_out_buffer and self.connected:
  134.             
  135.             try:
  136.                 num_sent = self.send(self.ac_out_buffer[:obs])
  137.                 if num_sent:
  138.                     self.ac_out_buffer = self.ac_out_buffer[num_sent:]
  139.             except socket.error:
  140.                 why = None
  141.                 self.handle_error()
  142.                 return None
  143.             except:
  144.                 None<EXCEPTION MATCH>socket.error
  145.             
  146.  
  147.         None<EXCEPTION MATCH>socket.error
  148.  
  149.     
  150.     def discard_buffers(self):
  151.         self.ac_in_buffer = ''
  152.         self.ac_out_buffer = ''
  153.         while self.producer_fifo:
  154.             self.producer_fifo.pop()
  155.  
  156.  
  157.  
  158. class simple_producer:
  159.     
  160.     def __init__(self, data, buffer_size = 512):
  161.         self.data = data
  162.         self.buffer_size = buffer_size
  163.  
  164.     
  165.     def more(self):
  166.         if len(self.data) > self.buffer_size:
  167.             result = self.data[:self.buffer_size]
  168.             self.data = self.data[self.buffer_size:]
  169.             return result
  170.         else:
  171.             result = self.data
  172.             self.data = ''
  173.             return result
  174.  
  175.  
  176.  
  177. class fifo:
  178.     
  179.     def __init__(self, list = None):
  180.         if not list:
  181.             self.list = deque()
  182.         else:
  183.             self.list = deque(list)
  184.  
  185.     
  186.     def __len__(self):
  187.         return len(self.list)
  188.  
  189.     
  190.     def is_empty(self):
  191.         return not (self.list)
  192.  
  193.     
  194.     def first(self):
  195.         return self.list[0]
  196.  
  197.     
  198.     def push(self, data):
  199.         self.list.append(data)
  200.  
  201.     
  202.     def pop(self):
  203.         if self.list:
  204.             return (1, self.list.popleft())
  205.         else:
  206.             return (0, None)
  207.  
  208.  
  209.  
  210. def find_prefix_at_end(haystack, needle):
  211.     l = len(needle) - 1
  212.     while l and not haystack.endswith(needle[:l]):
  213.         l -= 1
  214.     return l
  215.  
  216.